package at.codebase.core.log;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import at.codebase.core.LinkCreator;
import at.codebase.db.dao.ELogMessageDao;
import at.codebase.db.entities.ELogMessage;
import at.codebase.db.entities.EUser;

/**
 * Provides a very basic logging interface for the application.
 * 
 * @author Daniel Marth <danielmarth@gmx.at>
 */
public class Log {

	public static int getLogMessageCount() {
		return ELogMessageDao.getLogMessageCount();
	}

	private static void log(final Long userId, final String message, final int logType) {
		ELogMessageDao.saveLogMessage(new ELogMessage(EUser.key(userId), logType, message));
	}

	public static void logItem(final Long userId, final String message) {
		log(userId, message, LogTypes.ITEM);
	}

	public static void logUser(final Long userId, final String message) {
		log(userId, message, LogTypes.USER);
	}

	public static void logAdmin(final Long userId, final String message) {
		log(userId, message, LogTypes.ADMIN);
	}

	public static void logVirtualShopChanged(final Long userId, final String message) {
		log(userId, message, LogTypes.VSHOP);
	}

	public static void logMessage(final Long userId, final String message) {
		log(userId, message, LogTypes.MESSAGE);
	}

	public static List<ELogMessage> getLogMessages(final int offset,
			final int limit) {
		return ELogMessageDao.getLogMessages(offset, limit);
	}
	
	public static List<ELogMessage> getLogMessages(final int logType, final long userId, final int offset,
			final int limit) {
		return ELogMessageDao.getLogMessagesFilter(logType, userId, offset, limit);
	}

	public static String parseLogMessage(ELogMessage logMessage) {
		String message = logMessage.getText();
		//User
		Pattern tmpPattern = Pattern.compile("\\(userid=(\\d+)\\)");
		Matcher tmpMatcher = tmpPattern.matcher(message);
		StringBuffer stringBuffer = new StringBuffer();
		while (tmpMatcher.find()) {
			Long userId = new Long(tmpMatcher.group(1));
			tmpMatcher.appendReplacement(stringBuffer,
					Matcher.quoteReplacement(LinkCreator.userLink(userId, true)));
		}
		tmpMatcher.appendTail(stringBuffer);
		message = stringBuffer.toString();
		
		// Item
		tmpPattern = Pattern.compile("\\(itemid=(\\d+)\\)");
		tmpMatcher = tmpPattern.matcher(message);
		stringBuffer = new StringBuffer();
		while (tmpMatcher.find()) {
			Long itemId = new Long(tmpMatcher.group(1));
			tmpMatcher.appendReplacement(stringBuffer,
					Matcher.quoteReplacement(LinkCreator.itemLink(itemId, true)));
		}
		tmpMatcher.appendTail(stringBuffer);
		message = stringBuffer.toString();
		
		// VShop
		tmpPattern = Pattern.compile("\\(vshopid=(\\d+)\\)");
		tmpMatcher = tmpPattern.matcher(message);
		stringBuffer = new StringBuffer();
		while (tmpMatcher.find()) {
			Long vshopId = new Long(tmpMatcher.group(1));
			tmpMatcher.appendReplacement(stringBuffer,
					Matcher.quoteReplacement(LinkCreator.vShopLink(vshopId, true)));
		}
		tmpMatcher.appendTail(stringBuffer);
		message = stringBuffer.toString();
		
		// Image
		tmpPattern = Pattern.compile("\\(imageid=(\\d+)\\)");
		tmpMatcher = tmpPattern.matcher(message);
		stringBuffer = new StringBuffer();
		while (tmpMatcher.find()) {
			Long imageId = new Long(tmpMatcher.group(1));
			tmpMatcher.appendReplacement(stringBuffer,
					Matcher.quoteReplacement(LinkCreator.imageLink(imageId, true)));
		}
		tmpMatcher.appendTail(stringBuffer);
		message = stringBuffer.toString();
		
		
		return message;
	}

	public static int getLogMessageCountByType(final int logType, final long userId) {
		return ELogMessageDao.getLogMessageCountFilter(logType, userId);
	}

}